package com.example.once.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.once.common.Result;
import com.example.once.entity.Menu;
import com.example.once.entity.User;
import com.example.once.mapper.UserMapper;
import com.example.once.common.PageQuery;
import com.example.once.service.MenuService;
import com.example.once.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author xinTong
 * @since 2023-12-12
 */
@RestController
@RequestMapping("/user")
@Slf4j

public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private MenuService menuService;

    /**
     * 查询
     *
     * @return
     */
    @GetMapping("/select")
    public List<User> list() {
        return userService.list();
    }

    @GetMapping("/selectByNo")
    public List listByNo() {

        ArrayList<User> list = (ArrayList<User>) userService.list();

        ArrayList listNo = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            listNo.add(list.get(i).getNo());
        }
        System.out.println(listNo.size());

        return listNo;
    }

    @GetMapping("/findByNo")
    public Result listByNo(String no) {

        ArrayList<User> list = (ArrayList<User>) userService.list();

        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getNo().equals(no)) {

                System.out.println("!!!!!!!!!!!!");

            } else {
                System.out.println(list.get(i).getNo());
                System.out.println("无重复");
            }
        }
        return Result.suc(list);
    }

    /**
     * 新增
     */
    @PostMapping("/save")
    public Result save(@RequestBody User user) {
        return userService.save(user) ? Result.suc() : Result.fail();

    }

    /**
     * 修改或者新增
     */
    @PostMapping("/update1")
    public boolean update1(@RequestBody User user) {
        return userService.saveOrUpdate(user);
    }

    @PostMapping("/update")
    public Result update(@RequestBody User user) {
        return userService.updateById(user) ? Result.suc() : Result.fail();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    public boolean delete(Integer id) {
        return userService.removeById(id);
    }

    @GetMapping("/deleteById")
    public Result delete1(@RequestParam String id) {
        return userService.removeById(id) ? Result.suc() : Result.fail();
    }

    /**
     * 分页查询---实例
     */
    @PostMapping("/listPage")
    public Result pageQuery(@RequestBody PageQuery query) {


//        // 代码太过于繁杂，已移至接口实现类中
//        IPage<Map<String, Object>> select = userService.select(query);
//
//        System.out.println("总页数： "+select.getPages());
//        System.out.println("总记录数： "+select.getTotal());
//
//        PageBean pageBean = new PageBean(select.getTotal(),select.getRecords());
//        return Result.success(pageBean);

        HashMap param = query.getParam();
        String name = (String) param.get("name");
        String sex = (String) param.get("sex");
        String roleId = (String) param.get("roleId");

        IPage<User> page = new Page<>(query.getPageNum(), query.getPageSize());

        LambdaQueryWrapper<User> userLambdaQueryWrapper = Wrappers.lambdaQuery();
        if (StringUtils.isNotBlank(name)) {
            userLambdaQueryWrapper.like(User::getName, name);

        }
        if (StringUtils.isNotBlank(sex)) {
            userLambdaQueryWrapper.eq(User::getSex, sex);
        }
        if (StringUtils.isNotBlank(roleId)) {
            userLambdaQueryWrapper.eq(User::getRoleId, roleId);
        }

        IPage<User> iPage = userMapper.selectPage(page, userLambdaQueryWrapper);
        System.out.println("总页数： " + page.getPages());
        System.out.println("总记录数： " + page.getTotal());

        return Result.suc(iPage.getRecords(), iPage.getTotal());
    }

    @PostMapping("/login")
    public Result login(@RequestBody User user) {
        List<User> list = userService.lambdaQuery()
                .eq(User::getNo, user.getNo())
                .eq(User::getPassword, user.getPassword())
                .list();

        if (list.size()>0){
            User users = list.get(0);
            List menuList = menuService.lambdaQuery()
                    .like(Menu::getMenuRight, users.getRoleId())
                    .list();
            HashMap res = new HashMap<>();
            res.put("user",users);
            res.put("menu",menuList);

            //先执行,其他return不执行了
            return Result.suc(res);

        }
        return Result.fail();
    }
}

